package com.cn.zaprk;

import com.cn.zaprk.beans.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @PackageName: com.cn.zaprk
 * @ClassName: ServletDemo01
 * @Description:
 * @author: RZS
 * @date: 2022/4/22  8:56
 */
@WebServlet("/sd1.do")
public class ServletDemo01 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取session对象
        HttpSession session = request.getSession();
        // 绑定数组类型的数据到session域中
        String str[] = {"java", "PHP", "Python"};
        session.setAttribute("array", str);
        // 绑定集合类型的数据到session域中
        List<Double> list = new ArrayList<>();
        list.add(6000.0);
        list.add(7000.0);
        list.add(10000.0);
        session.setAttribute("list", list);
        // 绑定MAP类型的数据到session域中
        Map<String, String> map = new HashMap<>();
        map.put("k1", "val1");
        map.put("k2", "val2");
        session.setAttribute("map", map);
        // 绑定User对象类型的数据到session域中
        User user = new User();
        user.setId(1);
        user.setUsername("张三");
        user.setPassword("123456");
        session.setAttribute("user", user);
        // 转发到JSP做处理
        request.getRequestDispatcher("jsp/demo03.jsp").forward(request, response);
    }
}
